package com.baoxin.chenxixc4coach;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;

import com.alibaba.sdk.android.oss.OSS;
import com.alibaba.sdk.android.oss.OSSClient;
import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider;
import com.alibaba.sdk.android.oss.common.auth.OSSStsTokenCredentialProvider;
import com.baoxin.chenxixc4coach.utils.Interfaces;
import com.baoxin.chenxixc4coach.utils.NetUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class MyApplication extends Application {

    private static Context context;

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();
        initOSS();
    }

    public static Context getContext() {
        return context;
    }

    public static String getToken() {
        SharedPreferences sp = context.getSharedPreferences("TokenSP", MODE_PRIVATE);
        String token = sp.getString("token", "");
        return token;
    }

    public static void saveToken(String token) {
        SharedPreferences sp = context.getSharedPreferences("TokenSP", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("token", token);
        editor.commit();
    }

    public static String getUserName() {
        SharedPreferences sp = context.getSharedPreferences("TokenSP", MODE_PRIVATE);
        String userName = sp.getString("userName", "");
        return userName;
    }

    public static String getPassword() {
        SharedPreferences sp = context.getSharedPreferences("TokenSP", MODE_PRIVATE);
        String password = sp.getString("password", "");
        return password;
    }

    public static void saveUser(String userName, String password) {
        SharedPreferences sp = context.getSharedPreferences("TokenSP", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("userName", userName);
        editor.putString("password", password);
        editor.commit();
    }

    public static OSS oss;
    String endpoint = "http://oss-cn-shanghai.aliyuncs.com";

    private void initOSS() {

        OSSCredentialProvider credentialProvider;
        try {
            NetUtil.get(context, Interfaces.OSS_STS, new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {

                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String result = response.body().string();
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        String accessKeyId = jsonObject.getString("accessKeyId");
                        String securityToken = jsonObject.getString("securityToken");
                        String accessKeySecret = jsonObject.getString("accessKeySecret");
                        OSSCredentialProvider credentialProvider = new OSSStsTokenCredentialProvider(accessKeyId, accessKeySecret, securityToken);
                        oss = new OSSClient(context, endpoint, credentialProvider);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
